package com.lee.rpc.protocal.netty.handler;

import com.lee.rpc.protocal.core.Header;
import com.lee.rpc.protocal.core.ReqType;
import com.lee.rpc.protocal.core.RpcProtocal;
import com.lee.rpc.protocal.core.RpcRequest;
import com.lee.rpc.protocal.core.RpcResponse;
import com.lee.rpc.protocal.spring.service.Mediator;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

public class ServerHandler extends SimpleChannelInboundHandler<RpcProtocal<RpcRequest>>{

	@Override
	protected void channelRead0(ChannelHandlerContext ctx, RpcProtocal<RpcRequest> msg) throws Exception {
		// TODO Auto-generated method stub
		Header header = msg.getHeader();
		header.setReqType(ReqType.RESPONSE.code());
		
		Object data = Mediator.getInstance().processor(msg.getContent()); 
		RpcResponse rpcResponse = new RpcResponse();
		rpcResponse.setData(data);
		rpcResponse.setMsg("success");
		
		//构建返回消息
		RpcProtocal<RpcResponse> rpcProtocal = new RpcProtocal<>();		
		rpcProtocal.setHeader(header);
		rpcProtocal.setContent(rpcResponse);
		
		ctx.writeAndFlush(rpcProtocal);
		 
        
	}

}
